/*
 * SyncResource.java
 *
 * Created on April 12, 2007, 1:39 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.atomojo.auth.service.app;

import java.sql.SQLException;
import java.util.UUID;
import java.util.logging.Level;
import org.atomojo.auth.service.db.AuthDB;
import org.atomojo.auth.service.db.Permission;
import org.restlet.Request;
import org.restlet.data.CharacterSet;
import org.restlet.data.MediaType;
import org.restlet.data.Status;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.ServerResource;

/**
 *
 * @author alex
 */
public class PermissionResource extends ServerResource
{
   
   AuthDB db;
   String name;
   String suuid;
   /** Creates a new instance of SyncResource */
   public PermissionResource() {
      setNegotiated(false);
   }

   protected void doInit() {
      db = (AuthDB)getRequest().getAttributes().get(AuthApplication.DB_ATTR);
      name = AuthApplication.getStringAttribute(getRequest(),"name",null);
      suuid = AuthApplication.getStringAttribute(getRequest(),"uuid",null);
   }
   
   public Representation get()
   {
      try {
         Permission p = fetch();
         if (p!=null) {
            Representation entity = new DBObjectRepresentation(MediaType.APPLICATION_XML,p);
            entity.setCharacterSet(CharacterSet.UTF_8);
            return entity;
         }
         getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
         return null;
      } catch (SQLException ex) {
         getContext().getLogger().log(Level.SEVERE,"Cannot get permission from database.",ex);
         getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
         return new StringRepresentation("Exception during processing, see logs.");
      }
   }
   
   protected Permission fetch()
      throws SQLException
   {
      Permission p = null;
      if (name!=null) {
         p = db.getPermission(name);
      }
      if (suuid!=null) {
         UUID id = UUID.fromString(suuid);
         p = db.getPermission(id);
      }
      return p;
   }
   
   public Representation delete() {
      try {
         Permission p = fetch();
         if (p!=null) {
            p.delete();
            getResponse().setStatus(Status.SUCCESS_NO_CONTENT);
            return null;
         } else {
            getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
            return new StringRepresentation("Permission was not found.");
         }
      } catch (IllegalArgumentException ex) {
         getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
         return new StringRepresentation("Bad UUID value specified: "+ex.getMessage());
      } catch (SQLException ex) {
         getContext().getLogger().log(Level.SEVERE,"Database error during permission delete: "+ex.getMessage(),ex);
         getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
         return new StringRepresentation("Exception during processing, see logs.");
      }
   }
  
}
